﻿Imports Mystery
Public Class ContentListAttr
    Inherits MysteryDefaultClassAttribute

    Public Property implementing_type As Type

    Private generic As Type = GetType(ContentListCreator(Of ))
    Private creator As IControlCreator

    Public Overrides Sub setUp()
        If implementing_type IsNot Nothing Then
            implementing_type.GetInterfaceMap(GetType(IControlCreator))
        Else
            implementing_type = generic.MakeGenericType(used_in)
        End If

        creator = getGlobalObject(Of FastActivator).createInstance(implementing_type)

    End Sub

    Public Function createControl() As UserControl
        Return creator.createControl()
    End Function
End Class

Public Class ContentListContentGatherAttr
    Inherits MysteryDefaultClassAttribute

    Public Property implementing_type As Type

    Private activator As IActivator
    Private generic As Type = GetType(ContentTypeDefaultRetriver(Of ))

    Public Overrides Sub setUp()
        If implementing_type IsNot Nothing Then
            implementing_type.GetInterfaceMap(GetType(IContentRetriver))
        Else
            implementing_type = generic.MakeGenericType(used_in)
        End If

        activator = getGlobalObject(Of FastActivator).getActivator(implementing_type)

    End Sub

    Public Function retriveContents() As IEnumerable(Of IContent)
        Dim retriver As IContentRetriver = activator.createInstance()
        Return retriver.getAllTheContents
    End Function
End Class


Public Class ContentTypeDefaultRetriver(Of T As IContent)
    Implements IContentRetriver

    Private Class Comparer
        Implements IComparer(Of IContent)

        Private ref_text As ReferenceText = GetType(T).getFirstAttribute(Of ReferenceText)()

        Public Function Compare(x As Mystery.IContent, y As Mystery.IContent) As Integer Implements System.Collections.Generic.IComparer(Of Mystery.IContent).Compare
            Try
                Return String.Compare(ref_text.retrive(x), ref_text.retrive(y))
            Catch ex As Exception
                Return 0
            End Try
        End Function
    End Class


    Private ref_text As ReferenceText = GetType(T).getFirstAttribute(Of ReferenceText)()

    Public Function getAllTheContents() As IEnumerable(Of IContent) Implements Mystery.IContentRetriver.getAllTheContents
        Dim cc As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()
        Dim result As IEnumerable(Of IContent) = cc.getAll(Of T)()
        If ref_text Is Nothing Then Return result
        Dim sorted As New List(Of IContent)(result)
        sorted.Sort(New Comparer)
        Return sorted
    End Function

End Class

Public Class ContentListCreator(Of T As IContent)
    Implements IControlCreator

    Public Function createControl() As System.Windows.Controls.UserControl Implements IControlCreator.createControl
        Dim result As New ContentList
        result.create(Of T)(Nothing)
        getGlobalObject(Of InterfaceStatus)().registerContentShow(result)
        Return result
    End Function
End Class
